﻿var page = 1;
var pageSize = 30;
var action = "/report/query/self.do";
var allYear="/resources/js/views/allYear.do";

function query(curPage, pageSize) {
	
	curPage = parseInt(curPage);
	pageSize = parseInt(pageSize);
	var iname = $('#iname').textbox('getText');
	$('#page').val(curPage);
	$('#pageSize').val(pageSize);

	var formid=$("#formid").combobox('getValue');   	//报表（妇儿、社会）
	var years =$('#comboboxYear').combobox('getText');
	var yearList= [];
	if(years!=null ||years!='' || years!=undefined){
		yearList=years.split(',');
	}
	if(yearList.length>1){
		yearList.sort(funcSort);
	}

	var reportShowColumns = [
		defineTableReportAllColumns["iname"],
		defineTableReportAllColumns["code"],
		defineTableReportAllColumns["unit"],
		defineTableReportAllColumns["sname"]
	];
	
	for(var i=0; i< yearList.length; i++){
		var c = {title:yearList[i], field:yearList[i], width:80,align:'center',
				formatter: function (value, row, index) {
					if(value===null||value==null||value==""||value=="null"||value=="NULL"){
						return "";
					}
					return value;
		        }
		};
		reportShowColumns.push(c);
	}
	
	$.get(action, {"iname": iname, "years": years, "formsid": formid, "page": curPage, "pageSize": pageSize}, function (json) {
			if(json.code==200){
					var data = json.data;
					var total = data["size"];
					
					var totalPage = Math.ceil(total / pageSize);
					var start = (curPage-1) * pageSize;
					var dataLength = 0;
					
					data = data["results"];
					if(data!=null){
						dataLength = data.length;
					}
					
					var iHeight = document.documentElement.clientHeight;
		            $('#tt').datagrid( {
		                width : '100%',
		                height : iHeight-105,
		                columns: [
		                          reportShowColumns
		                ],
		                fitColumns: true,
		                singleSelect:true,// 是否单选
		                striped:true,
		                pageNumber: curPage,//显示数据列用
		                pageSize: pageSize,
		                pageList: [30,50,100,500],
		                rownumbers: true,
		                pagination: true,//打开翻页功能
		                data: data
		            });
		
		            var p = $('#tt').datagrid('getPager');
		            $(p).pagination({
		                total: total,
		                pageList: [30,50,100,500],
		                pageSize: pageSize,
		                pageNumber: curPage,
		                beforePageText: '第',
		                afterPageText: '页    共 ' + totalPage + ' 页',
		                displayMsg: '当前显示 ' + (start + 1) + '-' + (start + dataLength) + ' 条记录 共 ' + total + ' 条记录',
		                onSelectPage: function (curPage, pageSize) {
		                	query(curPage, pageSize);
		                },
		                onChangePageSize: function (ps) {
		                    pageSize = ps;
		                }
		            });
					try{window.parent.unmask();}catch(err){}
			}
	}, "json");
}

function funcSort (a,b) {
	return a-b;
}

function loadYear(){
	$.get(allYear, function (json) {
		$("#comboboxYear").combobox({
			textField:"text",
			valueField:"id",
			width:'100px',
			height:'28px',
			data:json,
			multiple:true,
			editable:false,
			formatter:function(row){
				var opts;
				if(row.select=="true"){
					row.selected=true;
					opts = "<input type='checkbox' name='year' checked='checked' id='"+row.id+"' value='"+row.id+"'>"+row.text+"</input>";
				}else{
					opts = "<input type='checkbox' name='year' id='"+row.id+"' value='"+row.id+"'>"+row.text+"</input>";
				}
				return opts;
			},
			panelHeight:"auto",
			panelWidth:'100px',
			panelAlign:'left',
			onSelect:function(rec) {
				oCheckbox = document.getElementById(rec.id);
				oCheckbox.checked=true;
				query(page,pageSize);
			},
			onUnselect:function(rec){
				oCheckbox = document.getElementById(rec.id);
				oCheckbox.checked=false;
				query(page,pageSize);
			}
			// ,
			// onLoadSuccess:function(){
			// 	queryList();
			// }
		});
	},'json');
}

$(document).ready(function () {
	loadYear();
	query(page,pageSize);
    departCommon.setSelfDepartId();
    checkYearBox();
});

var CRUD = {
    queryList: function () {
       try{window.parent.mask();}catch(err){}
       var pageNumber=$('#tt').datagrid('getPager').data("pagination").options.pageNumber;
       if(pageNumber==0){
           pageNumber=1;
       }
       var pageSize=$('#tt').datagrid('getPager').data("pagination").options.pageSize;
       query(pageNumber,pageSize);
    }
};

//年份复选框勾选显示隐藏列
function checkYearBox(){

	$("input[name='year']").click(function () { 
		if($(this).attr("checked")){
			var year=$(this).val()
			$("#tt").datagrid('showColumn', year);
		}else{
			var year=$(this).val()
			$("#tt").datagrid('hideColumn', year);
		}
	});
}

//前端导出excel
function exportexcel() {
    var grid = $("#tt");
    var rows=grid.datagrid("getRows");					  //获得表格行数据
    var colums_all=grid.datagrid("options").columns;      		     //获得表格的所有列不包括冻结列
    var obj = $.ExportExcelDlg({
        HeadInfo: colums_all,    //表头栏
        RowInfo: rows,		   //数据航
        FooterInfo: grid.datagrid("getFooterRows"),
        RowStart: 2,		   //导出行位置
        ColumStart: 2,		   //导出列位置
        Swf: 'ExportExcel.swf'
    });
    obj.ExportExcelDlg('open');
}

window.onresize = function () {
	onresizes();              //各容器自适应
}

function onresizes(){
	var iHeight = document.documentElement.clientHeight;
	$("#tt").datagrid('resize',{height:iHeight-105+'px'});
}
